package cn.edu.niit.alienlab.jshttplessonserver.service.impl;

import cn.edu.niit.alienlab.jshttplessonserver.dao.DirectorInfoDao;
import cn.edu.niit.alienlab.jshttplessonserver.dao.MovieDao;
import cn.edu.niit.alienlab.jshttplessonserver.dao.MovieRatingDao;
import cn.edu.niit.alienlab.jshttplessonserver.dao.Movie_infoDao;
import cn.edu.niit.alienlab.jshttplessonserver.domain.DirectorInfo;
import cn.edu.niit.alienlab.jshttplessonserver.domain.MovieRatings;
import cn.edu.niit.alienlab.jshttplessonserver.domain.MovieInfo;
import cn.edu.niit.alienlab.jshttplessonserver.domain.TypeInfo;
import cn.edu.niit.alienlab.jshttplessonserver.dto.CategoryDto;
import cn.edu.niit.alienlab.jshttplessonserver.dto.MovieInfoDto;
import cn.edu.niit.alienlab.jshttplessonserver.dto.YearDto;
import cn.edu.niit.alienlab.jshttplessonserver.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("MovieService")
public class MovieServiceImpl implements MovieService {
    @Autowired
    private MovieDao movieDao;

    @Autowired
    MovieRatingDao movieRatingDao;

    @Autowired
    private Movie_infoDao movieInfoDao;
    @Autowired
    private DirectorInfoDao directorInfoDao;


    @Override
    public List<CategoryDto> findAllCategory() {
        return movieDao.findAllCategory();
    }

    @Override
    public List<YearDto> findAllYear() {
        return movieDao.findAllYear();
    }

    @Override
    public Page<MovieInfoDto> findMovie(String keyword, Integer movie_year, String[] categories, Integer pageNum, Integer pageSize) {
        Integer start=pageNum*pageSize;
        List<MovieInfoDto> movie = movieDao.findMovieList(keyword, movie_year, categories,start,pageSize);
        Integer totalCount=movieDao.findMovieListCount(keyword,movie_year,categories);
        Page<MovieInfoDto> result= new PageImpl<>(movie, PageRequest.of(pageNum,pageSize),totalCount);
        return result;
    }

    @Override
    public List<MovieRatings> selectRatingsByMovieId(Integer movie_id) {
        List<MovieRatings> list = new ArrayList<>();
        list = movieRatingDao.selectRatingsByMovieId(movie_id);
        return list;
    }
    @Override
    public List<MovieInfo> findMovieByMovieID(Integer movie_id) {
        List<MovieInfo> list = new ArrayList<MovieInfo>();
        list = movieInfoDao.findMovieByMovieID(movie_id);
        return list;
    }

    @Override
    public List<DirectorInfo> getAllDirectors(String keyword) {
        return this.directorInfoDao.selectAllDirectors(keyword);
    }

    @Override
    public MovieInfo createMovie(MovieInfo movieInfo) throws Exception {
        if(movieInfo.getMovieId()==null){
            throw new Exception("请指定影片id");
        }
        if(movieInfo.getDirectorInfo()!=null && movieInfo.getDirectorInfo().getDirectorId()!=null){
            movieInfo.setDirectorId(movieInfo.getDirectorInfo().getDirectorId());
        }
        Integer rowCount=this.movieDao.createMovie(movieInfo);
        //完成主题信息保存后，保存分类信息
        if(rowCount>0){
            List<TypeInfo> types=movieInfo.getTypeInfoList();
            if(types!=null && types.size()>0){
                for(int i=0;i<types.size();i++){
                    TypeInfo typeInfo=types.get(i);
                    this.movieDao.addMovieType(movieInfo.getMovieId(),typeInfo.getTypeId());
                }
            }
            return movieInfo;
        }
        return null;
    }

    public MovieInfo updateMovie(MovieInfo movieInfo){
        if(movieInfo.getDirectorInfo()!=null && movieInfo.getDirectorInfo().getDirectorId()!=null){
            movieInfo.setDirectorId(movieInfo.getDirectorInfo().getDirectorId());
        }
        Integer rowCount=this.movieDao.updateMovie(movieInfo);
        //完成主题信息保存后，保存分类信息
        if(rowCount>0){
            this.movieDao.deleteMovieType(movieInfo.getMovieId());
            List<TypeInfo> types=movieInfo.getTypeInfoList();
            if(types!=null && types.size()>0){
                for(int i=0;i<types.size();i++){
                    TypeInfo typeInfo=types.get(i);
                    this.movieDao.addMovieType(movieInfo.getMovieId(),typeInfo.getTypeId());
                }
            }
        }
        return movieInfo;
    }

    @Override
    public boolean deleteMovie(Integer movieId) {
        Integer result=this.movieDao.deleteMovie(movieId);
        return result>0;
    }
}
